﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TraderControl.cs" company="">
//   
// </copyright>
// <summary>
//   The trader form.
// </summary>
// --------------------------------------------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Systemathics.FrameWork;

namespace Systemathics.UserControls
{
    [ToolboxBitmap(typeof(TraderControl), "UserControlsIcon.bmp")]
    public sealed partial class TraderControl : UserControl
    {
        private Dictionary<Guid, Instrument> Instruments;
        private Strategy strategy;

        public TraderControl()
        {
            InitializeComponent();

            Dock = DockStyle.Fill;
            DoubleBuffered = true;
        }

        private void TraderControl_Load(object sender, EventArgs e)
        {
            marketViewControl1.OnInstrumentSelected += MarketViewControl1OnInstrumentSelected;
        }
        public Strategy Strategy
        {
            set
            {
                strategy = value;
                Instruments = strategy.Instruments;
                if (Instruments != null)
                {
                    if (Instruments.Count > 0)
                    {
                        strategyViewControl1.SelectedStrategy = strategy;
                        ordersControl1.SelectedStrategy = strategy;
                        marketViewControl1.SelectedStrategy = strategy;
                        blotterControl1.SelectedInstrument = Instruments.Values.FirstOrDefault();
                    }
                }
                Text = strategy.strategy.Name + " - Pattern: " + strategy.strategy.Pattern + " - Version: " + strategy.strategy.Version + " - Created: " + strategy.strategy.CreationDate;
            }
            get{ return strategy;}
        }
        public void Clear()
        {
            marketViewControl1.Clear();
        }
        private void MarketViewControl1OnInstrumentSelected(object sender, FireMessageEventArgs e)
        {
            var id = (Guid)e.Message;

            ordersControl1.SelectedInstrument = Instruments[id];
            blotterControl1.SelectedInstrument = Instruments[id];
        }
        public void UpdateTraderControl()
        {
            strategyViewControl1.Update();
            marketViewControl1.Update();
            blotterControl1.Update();

            TCtoolStripLabel.Text = "-" + strategy.TransactionsCost_Traded.ToString("N0");
            InventoryTCtoolStripLabel.Text = "-" + strategy.TransactionsCost_Inventory.ToString("N0");
            NETPLtoolStripLabel.Text = strategy.NetPl_Traded.ToString("N0");
            InventoryNETPLtoolStripLabel.Text = strategy.NetPl_Inventory.ToString("N0");
        }
        private void HideShowtoolStripButton_Click(object sender, EventArgs e)
        {
            var bt = sender as ToolStripButton;
            if (bt.CheckState == CheckState.Checked)
                splitContainer1.Panel2Collapsed = true;
            else if (bt.CheckState == CheckState.Unchecked)
                splitContainer1.Panel2Collapsed = false;
        }
    }
}